/*
 * Created by JFormDesigner on Fri May 20 10:39:19 CST 2022
 */

package jw3.c10.view;

import com.mysql.fabric.xmlrpc.base.Member;
import jw3.c10.controllner.*;
import jw3.c10.model.Userinfo;

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Scanner;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class login extends JFrame {
    public login() {
        initComponents();

        setTitle("云卡用户管理系统");
        setSize(600,550);
        ImageIcon imgIcon=new ImageIcon("images\\i.gif");
        JLabel im=new JLabel();
        im.setIcon(imgIcon);
        im.setBounds(0,0,600,550);
        imgIcon.setImage(imgIcon.getImage().
                getScaledInstance(600,550,1));
        this.add(im);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        radioButton1.setContentAreaFilled(false);
        radioButton2.setContentAreaFilled(false);

    }

        //验证码
    public static String getRandomCode(int number) {
        String codeNum = "";
        int[] code = new int[3];
        Random random = new Random();
        for (int i = 0; i < number; i++) {
            int num = random.nextInt(10) + 48;
            int uppercase = random.nextInt(26) + 65;
            int lowercase = random.nextInt(26) + 97;
            code[0] = num;
            code[1] = uppercase;
            code[2] = lowercase;
            codeNum += (char) code[random.nextInt(3)];
        }

            return codeNum;
    }

    private void button1MouseClicked(MouseEvent e) {

        String Account=textField1.getText();
        String pwd=textField2.getText();
        String yzm=textField3.getText();

        UserinfoController uf=new UserinfoController();
        Userinfo u=new Userinfo();
        String i=null;
        //随机一个五位的验证码

                       int Jueisdiction=radioButton1.isSelected()?1:0;

    if (!Account.equals("") && !pwd.equals("")) {
         if (textField3.getText().equals(button3.getText())){
        if (uf.login(Account,pwd)) {

                            uf=new UserinfoController();
                              String user1= String.valueOf(uf.login(Account,pwd,Jueisdiction));
                              if (!user1.equals("")) {
                                 int UserID= UserinfoController.getIDByUser(Account);
                                  if (Jueisdiction == 1) {
                                      JOptionPane.showMessageDialog(null, "欢迎用户" + user1);
                                      new index();
                                 }
                      if (Jueisdiction==0){
                                      JOptionPane.showMessageDialog(null, "管理员，欢迎回来");
                                      new Administrator();
                                  }
                              }
            }else {
                JOptionPane.showMessageDialog(null,"登录失败");
            }
        } else {
            JOptionPane.showMessageDialog(null, "验证码不正确");
        }
    } else {
        JOptionPane.showMessageDialog(null, "账号或密码不能为空！");
    }
}


        private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here  忘记密码
        Loginforgeet loginforgeet=new Loginforgeet();
        loginforgeet.setVisible(true);
        loginforgeet.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here
//      验证码按钮
        button3.setText(getRandomCode(4));

    }

    private void button2MouseClicked(MouseEvent e) {
        Register register=new Register();
        register.setVisible(true);
        register.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }






    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();
        button4 = new JButton();
        textField1 = new JTextField();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        button3 = new JButton();
        radioButton1 = new JRadioButton();
        radioButton2 = new JRadioButton();

        //======== this ========
        setForeground(SystemColor.menuText);
        setBackground(Color.darkGray);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8d26\u53f7\uff1a");
        label1.setForeground(Color.white);
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(30, 50, 55, 25);

        //---- label2 ----
        label2.setText("\u5bc6\u7801\uff1a");
        label2.setForeground(Color.white);
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(30, 115, 55, 25);

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.setForeground(Color.black);
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(35, 295), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u6ce8\u518c");
        button2.setForeground(Color.black);
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(270, 295), button2.getPreferredSize()));

        //---- button4 ----
        button4.setText("\u5fd8\u8bb0\u5bc6\u7801");
        button4.setForeground(Color.black);
        button4.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button4MouseClicked(e);
            }
        });
        contentPane.add(button4);
        button4.setBounds(new Rectangle(new Point(140, 295), button4.getPreferredSize()));
        contentPane.add(textField1);
        textField1.setBounds(95, 55, 120, textField1.getPreferredSize().height);
        contentPane.add(textField2);
        textField2.setBounds(95, 115, 120, 30);

        //---- label3 ----
        label3.setText("\u9a8c\u8bc1\u7801\uff1a");
        label3.setForeground(Color.white);
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(25, 175, 65, 25);
        contentPane.add(textField3);
        textField3.setBounds(105, 175, 110, textField3.getPreferredSize().height);

        //---- button3 ----
        button3.setText("\u83b7\u53d6\u9a8c\u8bc1\u7801");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(240, 180), button3.getPreferredSize()));

        //---- radioButton1 ----
        radioButton1.setText("\u666e\u901a\u7528\u6237");
        radioButton1.setForeground(Color.white);
        contentPane.add(radioButton1);
        radioButton1.setBounds(30, 235, 95, radioButton1.getPreferredSize().height);

        //---- radioButton2 ----
        radioButton2.setText("\u7ba1\u7406\u5458");
        radioButton2.setForeground(Color.white);
        contentPane.add(radioButton2);
        radioButton2.setBounds(135, 235, 85, radioButton2.getPreferredSize().height);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

    }


    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JButton button1;
    private JButton button2;
    private JButton button4;
    private JTextField textField1;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button3;
    private JRadioButton radioButton1;
    private JRadioButton radioButton2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
